package com.bichengcao.googlecodejam.round1a2008;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Milkshakes {

	static int N;

	public static void main(String[] args) throws IOException {
		Input[] inputs = parseInput("src/com/bichengcao/googlecodejam/round1a2008/B-large-practice.in");
		Output[] outputs = process(inputs);
		writeOutput("src/com/bichengcao/googlecodejam/round1a2008/Milkshakes.out", outputs);
	}

	static class Input {

		int n;
		int m;
		Milk[][] milks;
	}

	static class Milk {

		int flavor;
		int malted;
	}

	static class Output {

		boolean isPossible = false;
		int[] flavors;
	}

	private static Input[] parseInput(String filename) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(filename));
		N = Integer.parseInt(in.readLine());
		Input[] array = new Input[N];

		for (int i = 0; i < N; i++) {
			Input input = array[i] = new Input();
			input.n = Integer.parseInt(in.readLine());
			input.m = Integer.parseInt(in.readLine());
			input.milks = new Milk[input.m][];
			for (int j = 0; j < input.m; j++) {
				String line = in.readLine();
				String[] splits = line.split(" ");
				int t = Integer.parseInt(splits[0]);
				input.milks[j] = new Milk[t];
				for (int k = 1; k <= 2 * t; k += 2) {
					input.milks[j][k/2] = new Milk();
					input.milks[j][k/2].flavor = Integer.parseInt(splits[k]);
					input.milks[j][k/2].malted = Integer.parseInt(splits[k + 1]);
				}
			}
			
		}

		in.close();
		return array;
	}

	private static Output[] process(Input[] inputs) {
		Output[] outputs = new Output[N];
		for (int i = 0; i < N; i++) {
			outputs[i] = process(inputs[i]);
		}
		return outputs;
	}

	private static Output process(Input input) {
		Output output = new Output();

		output.flavors = new int[input.n + 1];
		while (!output.isPossible) {
			for (int i = 0; i < input.m; i++) {
				boolean satisfied = false;
				for (int j = 0; j < input.milks[i].length; j++) {
					if (output.flavors[input.milks[i][j].flavor] == input.milks[i][j].malted) {
						satisfied = true;
						break;
					}
				}
				
				if (!satisfied) {
					for (int j = 0; j < input.milks[i].length; j++) {
						if (input.milks[i][j].malted == 1) {
							if (output.flavors[input.milks[i][j].flavor] == 1) {
								return output;
							} else {
								output.flavors[input.milks[i][j].flavor] = 1;
								break;
							}
						} else if (j == input.milks[i].length - 1) {
							return output;
						}
					}
					break;
				} else if (i == input.m - 1) {
					output.isPossible = true;
				}
			}
		}

		return output;
	}

	private static void writeOutput(String filename, Output[] outputs) throws IOException {
		PrintWriter out = new PrintWriter(new FileWriter(filename));
		for (int i = 0; i < N; i++) {
			out.print("Case #" + (i + 1) + ":");
			if (!outputs[i].isPossible) {
				out.println(" IMPOSSIBLE");
			} else {
				for (int j = 1; j < outputs[i].flavors.length; j++) {
					out.print(" " + outputs[i].flavors[j]);
				}
				out.println();
			}
		}
		out.close();
	}
}
